<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tomcat 持久化会话深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 1px solid transparent;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            border-color: #667eea;
        }
        
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow-x: auto;
            position: relative;
        }
        
        .code-block::before {
            content: 'XML';
            position: absolute;
            top: 10px;
            right: 15px;
            color: #667eea;
            font-size: 12px;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #667eea, transparent);
            margin: 3rem 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 50%;
            color: white;
            font-size: 24px;
            margin-bottom: 1rem;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center animate-fadeInUp">
            <h1 class="text-5xl md:text-6xl font-bold mb-6">
                <i class="fas fa-server mr-4"></i>Tomcat 持久化会话
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                深入探索企业级 Java 应用服务器的会话持久化机制，掌握高可用架构的核心技术
            </p>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover animate-fadeInUp">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">T</span>omcat 的持久化会话（Persistent Session）是现代 Web 应用架构中的关键技术之一。它通过将会话数据保存在持久化存储中，确保在服务器重启或会话超时后能够完美恢复用户状态。这种机制不仅提升了系统的可靠性，更是构建高可用、可扩展集群环境的基石。
            </p>
        </div>

        <!-- Architecture Visualization -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <h2 class="text-3xl font-bold mb-6 text-gradient">
                <i class="fas fa-sitemap mr-3"></i>架构概览
            </h2>
            <div class="mermaid">
                graph TB
                    A[用户请求] --> B[Tomcat 实例]
                    B --> C{会话管理器}
                    C --> D[内存会话存储]
                    C --> E[持久化存储]
                    E --> F[数据库存储]
                    E --> G[文件系统存储]
                    F --> H[(MySQL/PostgreSQL)]
                    G --> I[本地磁盘]
                    
                    style A fill:#667eea,stroke:#fff,color:#fff
                    style B fill:#764ba2,stroke:#fff,color:#fff
                    style C fill:#f093fb,stroke:#fff,color:#fff
                    style E fill:#4facfe,stroke:#fff,color:#fff
            </div>
        </div>

        <div class="section-divider"></div>

        <!-- Implementation Methods -->
        <div class="mb-12">
            <h2 class="text-4xl font-bold mb-8 text-center">
                <i class="fas fa-cogs mr-3 text-gradient"></i>实现方式
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <!-- Database Persistence -->
                <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-database"></i>
                    </div>
                    <h3 class="text-2xl font-bold mb-4">数据库持久化</h3>
                    <p class="text-gray-700 mb-6 leading-relaxed">
                        通过 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">JDBCStore</code> 接口，Tomcat 能够将会话数据安全地存储在关系型数据库中。这种方式特别适合需要跨服务器共享会话的集群环境。
                    </p>
                    
                    <div class="code-block p-6 text-sm">
                        <pre><code>&lt;Context&gt;
    &lt;Manager className="org.apache.catalina.session.PersistentManager"
             maxIdleSwap="30"
             maxIdleBackup="30"&gt;
        &lt;Store className="org.apache.catalina.session.JDBCStore"
               driverClassName="com.mysql.cj.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/tomcat_sessions"
               username="root"
               password="password"
               sessionTable="sessions"
               sessionIdColumn="session_id"
               sessionDataColumn="session_data"
               sessionValidColumn="session_valid"/&gt;
    &lt;/Manager&gt;
&lt;/Context&gt;</code></pre>
                    </div>
                    
                    <div class="mt-6 space-y-3">
                        <div class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span class="text-gray-700">支持多种数据库系统</span>
                        </div>
                        <div class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span class="text-gray-700">适合集群环境共享会话</span>
                        </div>
                        <div class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span class="text-gray-700">提供事务级别的数据安全</span>
                        </div>
                    </div>
                </div>

                <!-- File System Persistence -->
                <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-folder-open"></i>
                    </div>
                    <h3 class="text-2xl font-bold mb-4">文件系统持久化</h3>
                    <p class="text-gray-700 mb-6 leading-relaxed">
                        使用 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">FileStore</code> 将会话数据保存到本地文件系统。这种轻量级方案适合单机部署或开发测试环境。
                    </p>
                    
                    <div class="code-block p-6 text-sm">
                        <pre><code>&lt;Context&gt;
    &lt;Manager className="org.apache.catalina.session.PersistentManager"
             maxIdleSwap="30"
             maxIdleBackup="30"&gt;
        &lt;Store className="org.apache.catalina.session.FileStore"
               directory="session-store"/&gt;
    &lt;/Manager&gt;
&lt;/Context&gt;</code></pre>
                    </div>
                    
                    <div class="mt-6 space-y-3">
                        <div class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span class="text-gray-700">配置简单，易于实施</span>
                        </div>
                        <div class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span class="text-gray-700">无需额外的数据库依赖</span>
                        </div>
                        <div class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <span class="text-gray-700">适合小规模应用部署</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="section-divider"></div>

        <!-- Application Scenarios -->
        <div class="mb-12">
            <h2 class="text-4xl font-bold mb-8 text-center">
                <i class="fas fa-rocket mr-3 text-gradient"></i>核心应用场景
            </h2>
            
            <div class="space-y-6">
                <div class="bg-gradient-to-r from-purple-50 to-pink-50 rounded-2xl p-8 border border-purple-200">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-purple-500 rounded-full flex items-center justify-center text-white mr-4">
                            <i class="fas fa-network-wired"></i>
                        </div>
                        <h3 class="text-2xl font-bold">集群环境下的会话共享</h3>
                    </div>
                    <p class="text-gray-700 leading-relaxed">
                        在负载均衡的集群架构中，用户请求可能被路由到不同的服务器节点。持久化会话确保无论请求到达哪个节点，用户的会话状态都能被准确恢复，提供无缝的用户体验。这对于电商平台、在线银行等需要保持用户状态一致性的应用至关重要。
                    </p>
                </div>

                <div class="bg-gradient-to-r from-blue-50 to-cyan-50 rounded-2xl p-8 border border-blue-200">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-blue-500 rounded-full flex items-center justify-center text-white mr-4">
                            <i class="fas fa-shield-alt"></i>
                        </div>
                        <h3 class="text-2xl font-bold">高可用性保障</h3>
                    </div>
                    <p class="text-gray-700 leading-relaxed">
                        在企业级应用中，服务器可能因维护、故障或扩容而重启。持久化会话机制确保即使在这些情况下，用户的登录状态、购物车内容、表单数据等关键信息都不会丢失，极大提升了系统的可靠性和用户满意度。
                    </p>
                </div>

                <div class="bg-gradient-to-r from-green-50 to-emerald-50 rounded-2xl p-8 border border-green-200">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-green-500 rounded-full flex items-center justify-center text-white mr-4">
                            <i class="fas fa-clock"></i>
                        </div>
                        <h3 class="text-2xl font-bold">长时间会话管理</h3>
                    </div>
                    <p class="text-gray-700 leading-relaxed">
                        对于需要长时间保持会话的应用场景，如在线教育平台的课程学习进度、复杂的